﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CvPluginsStarter.Forms;
using NIPO.Controller;
using NIPO.Plugins;

namespace CvPluginsStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Do you want to load a NIPO instance?");
            if (Console.ReadKey(true).Key == ConsoleKey.Y)
            {
                Console.WriteLine("Please enter the filepath to load from");
                string name = Console.ReadLine();
                try
                {
                    NIPOController.Instance.LoadFromXML(name);
                }
                catch (Exception)
                {

                    try
                    {
                        NIPOController.Instance.Load(name);
                    }
                    catch (Exception)
                    {

                        Console.WriteLine("Could not load a NIPO instance from the specified file.");
                        return;
                    }
                }
            }
            else
            {
                Console.WriteLine("Welcome to NIPO. Searching for Input Plugins");
                LoadPluginByType(PlugInType.Input);
                Console.WriteLine("Searching for output plugins");
                LoadPluginByType(PlugInType.Output);
                Console.WriteLine("Add processing plugins?");
                if (Console.ReadKey(true).Key == ConsoleKey.Y)
                {
                    LoadPluginByType(PlugInType.Processing);
                }
                Console.WriteLine("Configure plugins?");
                if (Console.ReadKey(true).Key == ConsoleKey.Y)
                {
                    foreach (Plugin currentlySelectedPlugin in NIPOController.Instance.CurrentlySelectedPlugins)
                    {
                        if (currentlySelectedPlugin.ConfigurationObject == null)
                            continue;
                        MessageBox.Show(string.Format("Please configure {0}", currentlySelectedPlugin.Name));
                        ConfigForm cf = new ConfigForm(currentlySelectedPlugin.ConfigurationObject);
                        cf.ShowDialog();
                    }
                }
                Console.WriteLine("Do you want to save this configuration?");
                if (Console.ReadKey(true).Key == ConsoleKey.Y)
                {
                    Console.WriteLine("Please enter the filepath to save to");
                    string name = Console.ReadLine();
                    NIPOController.Instance.StoreToXML(new System.IO.FileInfo(name));
                }
            }
            Console.WriteLine("Press enter to start");
            Console.ReadLine();
            NIPO.Util.Logger.InitLogging();
            NIPOController.Instance.Start();
            Console.WriteLine("Press enter when NIPO is finished");
            Console.ReadLine();
            Console.ReadLine();
            Console.WriteLine("Finished");
        }

        private static void LoadPluginByType(PlugInType type)
        {
            List<Type> plugins = NIPOController.SearchPlugIns("NIPOCvPlugins.dll", type);
            if (plugins == null || plugins.Count == 0)
            {
                Console.WriteLine("No {0} plugins found", type.ToString());
                return;
            }
            do
            {
                if (plugins.Count == 1)
                {
                    Console.WriteLine("Found one {1}plugin: {0}.\nAdding to selected plugins", plugins[0].FullName, type);
                    NIPOController.Instance.AddPlugin((Plugin)plugins[0].Assembly.CreateInstance(plugins[0].FullName), type);
                }
                else
                {

                    Console.WriteLine("Choose from the following {0}plugins:", type);
                    for (int index = 0; index < plugins.Count; index++)
                    {
                        Type t = plugins[index];
                        Console.WriteLine("{0}: {1}", index, t.FullName);
                    }
                    int pluginIndex = Convert.ToInt32(Console.ReadLine());
                    NIPOController.Instance.AddPlugin((Plugin)plugins[pluginIndex].Assembly.CreateInstance(plugins[pluginIndex].FullName), type);
                }
                Console.WriteLine("Add another {0}plugin (y/n)?", type);
                ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
                Console.WriteLine();
                if (consoleKeyInfo.Key == ConsoleKey.N)
                    break;
            } while (true);
        }
    }
}
